package org.property.path;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;
import org.property.gui.Form;
import org.property.gui.PropertiesData;

/**
 * @author Yaroslav Boichuk
 * @since 26.08.09
 * 
 *        Class for working with directories. It scans directory.
 * 
 */

public class PathScanner implements IPathScanner {

	private static Logger log = Logger.getLogger(PathScanner.class);

	public static final String PATH_SEPARATOR = "/";

	// reference for ClientHandler
	private IClientHandler handlerData;

	// base directory
	private String baseDirectory;

	// parent folder
	private String fathersPath;

	// array with clients name
	private String[] clientNames;

	// private String[] clientNamesAll;

	private List<String> clientNamesAll;

	private PropertiesData propertiesData;

	// override method for parsing directories from folder and using client
	// handler for working with them
	public void parse(String baseDirectory, String fathersPath,
			IClientHandler hadlerData, PropertiesData propertiesData) {

		// set base directory to local field for using in that class
		this.baseDirectory = baseDirectory;
		this.fathersPath = fathersPath;

		// set reference to class with data of properties
		this.handlerData = hadlerData;

		this.propertiesData = propertiesData;

		String[] clientDontUse = propertiesData.getClientsDontUse().split(",");

		clientNamesAll = new LinkedList<String>();

		for (String client : clientDontUse) {
			this.clientNamesAll.add(client);
		}

		getClientsList(baseDirectory);
		setAlertNotFoundDir(baseDirectory);

		for (int i = 0; i < clientNames.length; i++) {
			getDisplaysDir(clientNames[i]);

		}

	}

	private void getDisplaysDir(String baseDir) {
		String directory = baseDirectory + PATH_SEPARATOR + baseDir
				+ PATH_SEPARATOR + "displays";
		File dir = new File(directory);

		if (dir.isDirectory()) {
			String[] listInDisplays = dir.list();
			for (int i = 0; i < listInDisplays.length; i++) {
				writePropertyToFile(baseDir + PATH_SEPARATOR + "displays"
						+ PATH_SEPARATOR + listInDisplays[i]);
			}

		}

	}

	private void writePropertyToFile(String dirWithFileProperty) {
		String directory = baseDirectory + PATH_SEPARATOR + dirWithFileProperty
				+ PATH_SEPARATOR + fathersPath + PATH_SEPARATOR;

		File dir = new File(directory);

		if (dir.isDirectory()) {
			// add property to file in directory
			handlerData.listThroughFiles(directory + PATH_SEPARATOR,
					propertiesData);

		}

	}

	// get list of folders in main root
	private String[] getClientsList(String baseDir) {
		File dir = new File(baseDir);
		List<String> clietReadyList = new LinkedList<String>();

		for (String client : dir.list()) {
			if (clientNamesAll.contains(client) == false) {
				clietReadyList.add(client);
			}
		}

		clientNames = new String[clietReadyList.size()];
		clietReadyList.toArray(clientNames);

		return clientNames;
	}

	private void setAlertNotFoundDir(String dirName) {
		File file = new File(dirName);
		if (file.exists() == false) {
			Form.setMessage("Can't find dir:" + baseDirectory);
			log.error("Directory " + file + " doesn't exists");
		}
	}

}
